from setting import *
from animation import *
from lifebar import *
from bossbullet import *
from missle import *
from guidedmissle import *


class BossPlane(pygame.sprite.Sprite):
    def __init__(self):
        self._layer = 2
        self.groups = allgroup, bossgroup
        pygame.sprite.Sprite.__init__(self, self.groups)
        #设置飞机动态图像
        self.animation = Animation()
        self.images = self.animation.load_images('images/boss/boss0_', 0, 4,
                                                 '.png')
        self.image = self.images[1]
        self.mask = pygame.mask.from_surface(self.image)
        # 飞机矩形
        self.rect = self.image.get_rect()
        self.bullet_speed = 4
        self.x_speed = 2
        self.y_speed = 2
        #默认移动方式
        self.motion_type = 1
        # 飞机子弹发射间隔 毫秒
        self.firetype = 0
        self.bullettype = 0
        self.bulletinterval = 200
        self.missleinterval = 500
        self.gmissleinterval = 2000
        # 飞机子弹发射当前时间
        self.bullet_start_time = pygame.time.get_ticks()
        self.missle_start_time = pygame.time.get_ticks()
        self.gmissle_start_time = pygame.time.get_ticks()

        self.HP = 500
        self.HPFULL = 500
        lifebar = Lifebar(self)

    def update(self):
        # self.HP -= 1
        if self.HP <= 0:
            self.kill()
        #更新飞机当前图像
        if random.randint(0, 50) == 1:
            self.motion_type = random.randint(0, 1)
            self.x_speed = random.randint(-2, 3)
            self.y_speed = random.randint(-3, 2)
        self.image = self.animation.get_current_image()
        if self.motion_type == 0:
            if self.rect.x + self.rect.width > SCENEWIDTH or self.rect.x < 0:
                self.x_speed = -self.x_speed
            # self.rect.y += self.y_speed
            self.rect.x += self.x_speed
        elif self.motion_type == 1:
            if self.rect.x + self.rect.width > SCENEWIDTH or self.rect.x < 0:
                self.x_speed = -self.x_speed
            if self.rect.y + self.rect.height > SCENEHEIGHT // 1.3 or self.rect.y < 0:
                self.y_speed = -self.y_speed
            self.rect.x += self.x_speed
            self.rect.y += self.y_speed

        self.fire_bullet(
            random.randint(0, 8), random.randint(0, 12),
            random.randint(500, 2000))
        self.fire_missle()
        self.fire_guided_missile()

    def fire_guided_missile(self, interval=2000):
        self.gmissleinterval = interval
        for hero in herogroup:
            hero_pos = vect(hero.rect.centerx, hero.rect.centery)
            for gmissle in bossGMgroup:
                gmissle.set_target_coordinate(hero_pos)
        current_time = pygame.time.get_ticks()
        pass_time = current_time - self.gmissle_start_time
        if pass_time < self.gmissleinterval:
            return
        self.gmissle_start_time = current_time
        gmissle = GuidedMissle(bossGMgroup)
        boss_fire_pos = vect(self.rect.midbottom[0],
                             self.rect.midbottom[1] - 100)
        pos_x = boss_fire_pos.x - gmissle.rect.width // 2
        pos_y = boss_fire_pos.y
        gmissle.set_pos(pos_x, pos_y)
        gmissle.set_target_coordinate(hero_pos)

    def fire_missle(self, interval=500):
        self.missleinterval = interval
        current_time = pygame.time.get_ticks()
        pass_time = current_time - self.missle_start_time
        if pass_time < self.missleinterval:
            return
        self.missle_start_time = current_time
        for hero in herogroup:
            missle = Missle(bossmisslegroup)
            hero_pos = vect(hero.rect.centerx, hero.rect.centery)
            boss_fire_pos = vect(self.rect.midbottom[0],
                                 self.rect.midbottom[1] - 100)
            pos_x = boss_fire_pos.x - missle.rect.width // 2
            pos_y = boss_fire_pos.y
            missle.set_pos(pos_x, pos_y)
            missle.set_target_coordinate(hero_pos)

    def fire_bullet(self, firetype=0, bullettype=0, interval=500):
        #时间判断，不到发射间隔，不射
        self.bulletinterval = interval
        current_time = pygame.time.get_ticks()
        pass_time = current_time - self.bullet_start_time
        if pass_time < self.bulletinterval:
            return
        self.bullet_start_time = current_time

        self.firetype = firetype
        self.bullettype = bullettype

        #boss 子弹的发射位置当然是下部中心
        boss_fire_pos = vect(self.rect.midbottom[0],
                             self.rect.midbottom[1] - 100)
        #单颗子弹
        if firetype == 0:
            bullet = BossBullet()
            bullet.set_bullet_type(bullettype)
            pos_x = boss_fire_pos.x - bullet.rect.width // 2
            pos_y = boss_fire_pos.y
            bullet.set_pos(pos_x, pos_y)
        # 两个
        elif firetype == 1:
            interval = -self.rect.width // 4
            for v in range(0, 2):
                bullet = BossBullet()
                bullet.set_bullet_type(bullettype)
                pos_x = boss_fire_pos.x - bullet.rect.width // 2 + interval
                pos_y = boss_fire_pos.y
                bullet.set_pos(pos_x, pos_y)
                interval = self.rect.width // 4
        #三个
        elif firetype == 2:
            interval = -self.rect.width // 4
            for v in range(0, 3):
                bullet = BossBullet()
                bullet.set_bullet_type(bullettype)
                pos_x = boss_fire_pos.x - bullet.rect.width // 2 + interval
                pos_y = boss_fire_pos.y
                bullet.set_pos(pos_x, pos_y)
                interval += self.rect.width // 4
        # 向下30度角
        elif firetype == 3:
            for angle in range(75, 106, 10):
                bullet = BossBullet()
                bullet.set_bullet_type(bullettype)
                pos_x = boss_fire_pos.x
                pos_y = boss_fire_pos.y
                bullet.set_speed(self.bullet_speed, angle)
                bullet.set_pos(pos_x, pos_y)
        #向下 60度角
        elif firetype == 4:
            for angle in range(60, 121, 10):
                bullet = BossBullet()
                bullet.set_bullet_type(bullettype)
                pos_x = boss_fire_pos.x
                pos_y = boss_fire_pos.y
                bullet.set_speed(self.bullet_speed, angle)
                bullet.set_pos(pos_x, pos_y)
        #向下 90度角
        elif firetype == 5:
            for angle in range(45, 136, 10):
                bullet = BossBullet()
                bullet.set_bullet_type(bullettype)
                pos_x = boss_fire_pos.x
                pos_y = boss_fire_pos.y
                bullet.set_speed(self.bullet_speed, angle)
                bullet.set_pos(pos_x, pos_y)
        #向下 180度
        elif firetype == 6:
            for angle in range(0, 181, 10):
                bullet = BossBullet()
                bullet.set_bullet_type(bullettype)
                pos_x = boss_fire_pos.x
                pos_y = boss_fire_pos.y
                bullet.set_speed(self.bullet_speed, angle)
                bullet.set_pos(pos_x, pos_y)
        #360度
        elif firetype == 7:
            for angle in range(0, 360, 10):
                bullet = BossBullet()
                bullet.set_bullet_type(bullettype)
                pos_x = boss_fire_pos.x
                pos_y = boss_fire_pos.y
                bullet.set_speed(self.bullet_speed, angle)
                bullet.set_pos(pos_x, pos_y)
        #一顿乱射
        else:
            n = random.randint(10, 50)
            for v in range(0, n):
                if random.randint(0, 4) == 1:
                    bullet = BossBullet()
                    bullet.set_bullet_type(bullettype)
                    pos_x = boss_fire_pos.x - bullet.rect.width // 2
                    pos_y = boss_fire_pos.y
                    bullet.set_pos(pos_x + random.randint(-200, 200),
                                   pos_y + random.randint(-100, 100))
